// TradeSlotContainer.cpp: implementation of the CTradeSlotContainer class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "TradeSlotContainer.h"
#include "TradeSlot.h"
#include "SCItemSlot.h"
#include "Player.h"

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

TradeSlotContainer::TradeSlotContainer()
{
	Init( MAX_TRADE_SLOT_NUM/2, SI_TRADE );
}

TradeSlotContainer::~TradeSlotContainer()
{
	
}

VOID TradeSlotContainer::Init( POSTYPE MaxSlotSize, SLOTIDX SlotIdx )
{
	m_nSlotNum		= 0;
	m_SlotIdx		= SlotIdx;
	m_nMaxSlotNum	= MaxSlotSize;
	m_ppSlotArray	= new SCSlot*[MaxSlotSize];
	for(POSTYPE i=0;i<MaxSlotSize;++i)
		m_ppSlotArray[i] = new TradeSlot;
	ClearAll();
}

VOID TradeSlotContainer::Init( Player * pPlayer, Player * pCounterPlayer, DWORD key )
{
	m_pPlayer = pPlayer;
	m_pCounterPlayer = pCounterPlayer;
	m_dwKey = key;
	m_Money = 0;
	m_State = READY;
	ClearAll();
}

BOOL TradeSlotContainer::PutMoney( MONEY & IN money )
{
	if( m_pPlayer->GetMoney() < money )
		return FALSE;
	m_Money = money;

	return TRUE;
}

BOOL TradeSlotContainer::TakeMoney( MONEY & IN money )
{
	if( m_Money < money )
		return FALSE;

	m_Money -= money;

	return TRUE;
}

BOOL TradeSlotContainer::PutItem( SCItemSlot & IN slot, POSTYPE & OUT PutPos )
{
	if( slot.IsLocked() )
		return FALSE;

	ASSERT( m_State == READY );
	TradeSlot tradeSlot;
	tradeSlot.SetLinkItem( &slot );

	if( !GetEmptyPos(PutPos) )
		return FALSE;

	BOOL rt = InsertSlot( PutPos, tradeSlot );
	ASSERT( rt );

	return TRUE;
}

VOID TradeSlotContainer::TakeItem( POSTYPE pos )
{
	ASSERT( m_State == READY );

	DeleteSlot( pos, NULL );
}

VOID TradeSlotContainer::GetOriginalSlotItem( POSTYPE trade_pos, ITEMSTREAMEX & OUT ItemStream )
{
	TradeSlot & slot = (TradeSlot &)GetSlot(trade_pos);
	ASSERT( slot.GetLinkItem() );
	slot.GetLinkItem()->CopyOut( ItemStream );
}

BOOL TradeSlotContainer::IsEmpty( POSTYPE AtPos )
{
	return ( m_ppSlotArray[AtPos]->GetSerial() == 0 );
}